package org.example.api;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class redis {

    public static  void test1(Jedis jedis) {
        // get set
        // 1. 清空数据库
        jedis.flushDB();
        // 2. set
        // jedis.set("key","111");
        //jedis.set("key2","222");
        SetParams params = new SetParams();
        params.ex(10);
        params.nx();
        jedis.set("key4","444");
        String value = jedis.get("key3");
        System.out.println("value:"+ value);
    }
    public static void test2(Jedis jedis) {
        System.out.println("exists 和 del");
        jedis.del("key4");
        boolean key2 = jedis.exists("key4");
        System.out.println(key2);
    }
    public static void test3(Jedis jedis) {
        System.out.println();
        jedis.set("key1","111");
        SetParams setParams = new SetParams();

        Set<String> keys = jedis.keys("* ");
        System.out.println(keys);
    }
    public static void test4(Jedis jedis)  {
        System.out.println("expire and ttl");
        jedis.flushDB();
        jedis.set("key","111");
        jedis.expire("key",10);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long time = jedis.ttl("key");
        System.out.println(time);

    }
    public static void test5(Jedis jedis) {
        System.out.println("type");
        jedis.zadd("key2",99,"zhangsan");
        String key = jedis.type("key2");
        System.out.println(key);
    }

    public static void main(String[] args) {
        // 连接服务器  JedisPoll
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 去一个连接池
        try(Jedis jedis = jedisPool.getResource()) {
            // redis的各种命令 对应 jedis 对象的方法
//            String pong = jedis.ping();
//            System.out.println(pong);
            // test1(jedis);
            //test2(jedis);
            //test3(jedis);
           //test4(jedis);
            test5(jedis);
        }
    }
}
